package me.zfz.business.service;

import com.mixsmart.security.SecurityUtils;
import com.mixsmart.utils.LoggerUtils;
import com.mixsmart.utils.StringUtils;
import me.zfz.base.bean.SmartResponse;
import me.zfz.base.bean.TreeProp;
import me.zfz.base.constant.IConstant;
import me.zfz.base.exception.DaoException;
import me.zfz.base.exception.ServiceException;
import me.zfz.base.helper.ObjectHelper;
import me.zfz.base.helper.TreeCombinHelper;
import me.zfz.base.plugins.OrgUserZTreeData;
import me.zfz.base.plugins.ZTreeHelper;
import me.zfz.base.service.impl.MgrServiceImpl;
import me.zfz.business.bean.RegisterUser;
import me.zfz.business.bean.UserInfo;
import me.zfz.business.bean.UserSearchParam;
import me.zfz.business.bean.entity.StudentInfo;
import me.zfz.business.bean.entity.TNOrg;
import me.zfz.business.bean.entity.TNUser;
import me.zfz.business.constant.enums.OrgType;
import me.zfz.business.dao.impl.OrgDao;
import me.zfz.business.dao.impl.StudentInfoDao;
import me.zfz.business.dao.impl.UserDao;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.*;

/**
 * @author author
 */
@Service("stuService")
public class StudentInfoService extends MgrServiceImpl<StudentInfo> {

    @Autowired
    private StudentInfoDao studentInfoDao;
    @Autowired
    private UserDao userDao;

    /**
     * 根据searchParam参数查询信息
     *
     * @param searchParam
     * @param start
     * @param rows
     * @return
     * @throws ServiceException
     */
    public SmartResponse<Object> findAllObj(UserSearchParam searchParam, int start, int rows) throws ServiceException {
        SmartResponse<Object> smartResp = new SmartResponse<Object>();
        smartResp.setResult(IConstant.OP_NOT_DATA_SUCCESS);
        smartResp.setMsg(IConstant.OP_NOT_DATA_SUCCESS_MSG);
        try {
            List<Object> objs = studentInfoDao.queryObjPage(searchParam, start, rows);
            if (null != objs && objs.size() > 0) {
                smartResp.setResult(IConstant.OP_SUCCESS);
                smartResp.setMsg(IConstant.OP_SUCCESS_MSG);
                objs = ObjectHelper.handleObjDate(objs);
                smartResp.setDatas(objs);
                long total = studentInfoDao.queryObjCount(searchParam);
                smartResp.setTotalNum(total);
                smartResp.setTotalPage(getTotalPage(total, rows));
            }
        } catch (DaoException e) {
            throw new ServiceException(e.getMessage(), e.getCause());
        } catch (Exception e) {
            throw new ServiceException(e.getCause());
        }
        return smartResp;
    }


    /**
     * 保存用户信息
     *
     * @param registerUser
     * @return
     * @throws ServiceException
     */
    @Transactional(rollbackFor = Exception.class)
    public SmartResponse<String> save(RegisterUser registerUser) throws ServiceException {
        SmartResponse<String> smartResp = new SmartResponse<String>();
        try {
            if (null != registerUser) {
                boolean existUsername = userDao.isExistUsername(registerUser.getUsername());
                if (existUsername) {
                    smartResp.setResult(IConstant.OP_FAIL);
                    smartResp.setMsg("用户名已存在");
                    smartResp.setData(registerUser.getUsername());
                } else {
                    TNUser tnUser = new TNUser();
                    String md5Pwd = SecurityUtils.md5(registerUser.getPassword());
                    tnUser.setPassword(md5Pwd);
//                   "2")?"R-UU142509589602782910":"01337e1d2666412fa3d028136ff710f7";
                    tnUser.setRoleId(registerUser.getRoleType());
                    tnUser.setOrgId(registerUser.getOrgId());
                    tnUser.setFullName(registerUser.getFullName());
                    tnUser.setUsername(registerUser.getUsername());
                    tnUser.setEmail(null);
                    tnUser.setMobileNo(null);
                    tnUser.setQq(null);
                    tnUser.setRemark(null);
                    tnUser.setSortOrder(null);
                    tnUser.setAuditFlag("0");
                    tnUser.setState("1");
                    Serializable save = userDao.save(tnUser);
                    StudentInfo studentInfo = new StudentInfo();
                    studentInfo.setAuditFlag("0");
                    studentInfo.setUserId(save.toString());
                    studentInfo.setSduNo(registerUser.getStuNo());
                    studentInfoDao.save(studentInfo);
                    smartResp.setResult(IConstant.OP_SUCCESS);
                    smartResp.setMsg("学生信息添加成功");
                    smartResp.setData("");
                }
            }
        } catch (DaoException e) {
            throw new ServiceException(e.getMessage(), e.getCause());
        } catch (Exception e) {
            throw new ServiceException(e.getCause());
        }
        return smartResp;
    }

    @Override
    public SmartResponse<String> update(StudentInfo studentInfo) throws ServiceException {
        SmartResponse<String> smartResp = new SmartResponse<String>();
        try {
            StudentInfo studentInfoDb = studentInfoDao.find(studentInfo.getId());
            if (null != studentInfoDb && studentInfoDb.getId().equals(studentInfo.getId())) {
                studentInfoDb.setSduNo(studentInfo.getSduNo());
                studentInfoDb.setStartYear(studentInfo.getStartYear());
                studentInfoDb.setWorkFlag(studentInfo.getWorkFlag());
                studentInfoDb.setWorkProvince(studentInfo.getWorkProvince());
                studentInfoDb.setWorkCity(studentInfo.getWorkCity());
                studentInfoDb.setWorkCountry(studentInfo.getWorkCountry());
                studentInfoDb.setWorkAddrDetail(studentInfo.getWorkAddrDetail());
                studentInfoDb.setWorkCompany(studentInfo.getWorkCompany());
                studentInfoDb.setWorkJob(studentInfo.getWorkJob());
                studentInfoDb.setZyfx(studentInfo.getZyfx());
                studentInfoDb.setWorkSalary(studentInfo.getWorkSalary());
                studentInfoDb.setWxFlag(studentInfo.getWxFlag());
                studentInfoDb.setGjjFlag(studentInfo.getGjjFlag());
                studentInfoDb.setAuditFlag(studentInfo.getAuditFlag());
                studentInfoDao.update(studentInfoDb);
            }else{
                studentInfo.setUserId(studentInfoDb.getUserId());
                studentInfoDao.update(studentInfo);
            }
                smartResp.setResult(IConstant.OP_SUCCESS);
                smartResp.setMsg("学生信息修改成功");
                smartResp.setData(studentInfo.getId());
        } catch (DaoException e) {
            throw new ServiceException(e.getMessage(), e.getCause());
        } catch (Exception e) {
            throw new ServiceException(e.getCause());
        }
        return smartResp;
    }

    /**
     * 删除用户信息
     *
     * @param id
     * @return
     * @throws ServiceException
     */
    public SmartResponse<String> del(String id) throws ServiceException {
        SmartResponse<String> smartResp = new SmartResponse<String>();
        try {
            if (null != id) {
                if (studentInfoDao.delete(id)) {
                    smartResp.setResult(IConstant.OP_SUCCESS);
                    smartResp.setMsg("学生数据删除成功");
                }
            }
        } catch (DaoException e) {
            throw new ServiceException(e.getMessage(), e.getCause());
        } catch (Exception e) {
            throw new ServiceException(e.getCause());
        }
        return smartResp;
    }

    public StudentInfoDao getDao() {
        return (StudentInfoDao) super.getDao();
    }

}
